var Message = Backbone.Model.extend({
    urlRoot: '/api/messages',
    defaults:{
        content: 'test',
        type: 'demo'
    },
    parse: function(resp){
        return resp.data? resp.data: resp;
    },
    idAttribute: 'messageId'
});

var MessageCollection = Backbone.Collection.extend({
    url: '/api/messages',
    model: Message,
    parse:function(resp){
        return resp.data? resp.data: resp;
    }
});

//var message = new Message;
////新增model使用POST
////message.save();
//var message1 = new Message({messageId:1});
////查询model是，使用GET
//message1.fetch({
//    success:function(messge){
//        console.log(messge.toJSON());
//    }
//});


//修改model,patch为false时 PUT整个json到服务器
//message1.save({"content":"aa"},{patch: false});
//修改model,patch为false时 PATCH指定json字段到服务器
//message1.save({"content":"aa"},{patch: true});
//删除model，使用DELETE
//message1.destroy();

var messages = new MessageCollection;

//读取collection，使用get
messages.fetch({
    success: function(messages){

        console.log(messages.models[0].get("content"));
        console.log("1 messages length = "+ messages.length);

    },
    data:{
        page:1,
        limit:10
    }
});

messages.on('add',function(message){
   console.log('messages add event triggered');
});

//在collection新增model
var message2 =  messages.create({content:'333',type:'D'});

messages.on('remove',function(message){
    message.destroy({data:{type:1,fore:true}, processData: true});
});


setTimeout(function(){
    //删除列表指定记录
    messages.remove({messageId:1});
    message2.save();
    messages.set({messageId:2});
},4000);


